package cn.zl.springbootredis.redis;

import cn.zl.springbootredis.SpringbootRedisApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: zhanglang
 * @Date: 2020/9/7 11:08
 */
//启动Springboot应用
@SpringBootTest(classes = SpringbootRedisApplication.class)
@RunWith(SpringRunner.class)
public class TestStringRedisTemplate {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;//key ，value都是字符串

    //操作redis中key相关
    @Test
    public void testKey(){
//        stringRedisTemplate.delete("name");//删除一个key
        Boolean hasKey = stringRedisTemplate.hasKey("name");//判断某个key是否存在
        System.out.println(hasKey);
        DataType name = stringRedisTemplate.type("name");//判断key所对应值的类型
        System.out.println(name);
        Set<String> keys = stringRedisTemplate.keys("*");//获取redis中所有
        keys.forEach(key -> System.out.println("key = "+ key));
        Long expire = stringRedisTemplate.getExpire("name");//获取key的超时时间
        System.out.println(expire);//-1,代表永不超时；-2 key不存在，>=0 超时时间
        stringRedisTemplate.randomKey();//所及获取一个key
        stringRedisTemplate.rename("age","age1");//修改key的名字，要求key必须存在，不存在会报错
        stringRedisTemplate.renameIfAbsent("age","age1");//也是修改key的名字，但是会判断key是否存在
        stringRedisTemplate.move("name",1);//移动key到指定库
    }

    //操作redis中字符串,opsForValue实际就是操作redis中的String类型
    @Test
    public void testString(){
        stringRedisTemplate.opsForValue().set("name","小张");//设置 key value
        String name = stringRedisTemplate.opsForValue().get("name");//获取value
        System.out.println(name);
        stringRedisTemplate.opsForValue().set("code","2357",60, TimeUnit.SECONDS);//设置一个key的超时时间
        stringRedisTemplate.opsForValue().append("name","你是个好人");//追加
    }
    //操作redis中list类型，opsForList实际就是操作redis中list类型
    @Test
    public void testList(){
        stringRedisTemplate.opsForList().leftPush("names","小黑");//创建一个列表，并放入一个元素
        stringRedisTemplate.opsForList().leftPushAll("names","小花","小王","小三");
        ArrayList<Object> names = new ArrayList<>();
        names.add("xiaoming");
        names.add("xiaosan");
        stringRedisTemplate.opsForList().leftPushAll("names", String.valueOf(names));//创建一个列表，放入多个元素
        List<String> stringList = stringRedisTemplate.opsForList().range("names", 0, -1);//遍历list
        stringList.forEach( value -> System.out.println("value = "+ value));
        stringRedisTemplate.opsForList().trim("names",1,3);//截取指定区间的list,前三个，
    }

    //操作redis中set类型，opsForSet实际就是操作redis中set类型
    @Test
    public void testSet(){
        stringRedisTemplate.opsForSet().add("sets","张三","李三","xiaoming");//添加
        Set<String> sets = stringRedisTemplate.opsForSet().members("sets");//成员
        sets.forEach( value -> System.out.println("value = " +value));

        stringRedisTemplate.opsForSet().size("sets");//获取set集合元素个数
    }
    //操作redis中zset类型，opsForZSet实际就是操作redis中zset类型
    @Test
    public void testZset(){
        stringRedisTemplate.opsForZSet().add("zsets","张三",100);//创建并放入元素
        Set<String> zsets = stringRedisTemplate.opsForZSet().range("zsets", 1, -1);//指定范围查询
        zsets.forEach( value -> System.out.println("value = "+value));
        Set<ZSetOperations.TypedTuple<String>> zsets1 = stringRedisTemplate.opsForZSet().rangeByScoreWithScores("zsets", 0, 100);
        zsets1.forEach( typedTuple-> {
            System.out.println(typedTuple.getValue());
            System.out.println(typedTuple.getScore());
        });
    }
    //操作redis中Hash类型，opsForHash实际就是操作redis中Hash类型
    @Test
    public void testHash(){
        stringRedisTemplate.opsForHash().put("maps","name","张三");//创建一个hash类型，并放入key value
        HashMap<String, String> map = new HashMap<>();
        map.put("age","12");
        map.put("bir","2020-2-2");
        stringRedisTemplate.opsForHash().putAll("maps",map);//放入多个key value

        List<Object> values = stringRedisTemplate.opsForHash().multiGet("maps", Arrays.asList("name", "age"));//获取多个key
        String value = (String) stringRedisTemplate.opsForHash().get("maps","name");//获取hash中某个key的值
        List<Object> vals = stringRedisTemplate.opsForHash().values("maps");//获取所有values
        Set<Object> keys = stringRedisTemplate.opsForHash().keys("maps");//获取所有key
    }

}
